package com.hsogoo.ultraman.loadbanlance;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.google.common.collect.Lists;

/**
 * Created by za-huangsaigang on 2018/9/19.
 */
public class RoundRobinLoadBalancer implements LoadBalancer {

    private List<String> serverList = Lists.newArrayList();
    private final static AtomicInteger next = new AtomicInteger(0);

    public RoundRobinLoadBalancer(List<String> serverList){
        this.serverList = serverList;
    }

    @Override
    public String select() {
        String[] serverArray = serverList.toArray(new String[serverList.size()]);
        return serverArray[next.getAndIncrement() % serverArray.length];
    }

}
